use serde::{Deserialize, Serialize};
use serde_json;

#[derive(Serialize, Deserialize, Default, Clone,Debug)]
pub struct Work {
    pub id: i8,
    pub title: String,
    pub detail: String,
    pub status: i8, //0 未完成， 1 完成，-1 删除 2 超时
    pub modify_date: String,
}

impl Work {
    pub fn new(id: i8, title: String, detail: String) -> Self {
        Self {
            id,
            title,
            detail,
            status: 0,
            modify_date: "2024-11-05: 19:00:00".to_owned(),
        }
    }

    pub fn update(&mut self, id: i8, title: String, detail: String, status: i8) -> (bool, &str) {
        if title.is_empty() {
            return (false, "The title must not be blank!");
        }
        if status != 0 && status != 1 && status != -1 {
            return (
                false,
                "The status is wrong, please contact the author to check!",
            );
        }
        println!("update the work...");
        (true, "OK")
    }

    pub fn delete(&mut self, id: i8) -> (bool, &str) {
        println!("delete work...");
        //更新状态，修改时间
        (true, "OK")
    }

    pub fn to_json(&self) -> String {
        serde_json::to_string(&self).unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_to_json() {
        let work = Work::new(0, String::from("test work"), String::from("detail test"));
        println!(
            "the json string : {}",
            serde_json::to_string(&work).unwrap()
        );
        println!("the json string:   {}   ", work.to_json());
    }
}
